home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4889 / 4889.xpi / chrome / picnik.jar / content / screengrab.js < prev    next >
Encoding:
Text File  |  2007-05-07  |  6.1 KB  |  226 lines

  1.  
  2. var picnikScreenGrab = {
  3.     onLoad: function()
  4.     {
  5.         // check if this firefox has canvas AND toDataURL
  6.         var has_canvas = true;
  7.         try
  8.         {
  9.             var canvas = document.createElementNS('http://www.w3.org/1999/xhtml','canvas');
  10.             if( canvas == null )
  11.                 throw Exception;
  12.             if( !("toDataURL" in canvas) )
  13.                 throw Exception;
  14.         }
  15.         catch(e)
  16.         {
  17.             has_canvas = false;
  18.         }
  19.  
  20.         if( !has_canvas )
  21.         {
  22.             var e = document.getElementById("picnik-tool-menu");
  23.             if(e)
  24.                 e.hidden=true;
  25.  
  26.             e = document.getElementById("picnik-button");
  27.             if(e)
  28.                 e.hidden=true;
  29.  
  30.             e = document.getElementById("picnik-ctx-grab");
  31.             if(e)
  32.                 e.hidden=true;
  33.         }
  34.  
  35.     },
  36.  
  37.     grabFull: function()
  38.     {
  39.         var h = 0;
  40.         var w = 0;
  41.         if( window.content.document.compatMode == "CSS1Compat" )
  42.         {
  43.             h = window.content.document.documentElement.scrollHeight;
  44.             w = window.content.document.documentElement.scrollWidth;
  45.         }
  46.         else
  47.         {
  48.             h = window.content.document.body.scrollHeight;
  49.             w = window.content.document.body.scrollWidth;
  50.         }
  51.         this.grab(window.content,0,0,w,h);
  52.     },
  53.  
  54.     grabVisible: function()
  55.     {
  56.         var x = window.content.scrollX;
  57.         var y = window.content.scrollY; 
  58.         var h = 0;
  59.         var w = 0;
  60.         if( window.content.document.compatMode == "CSS1Compat" )
  61.         {
  62.             h = window.content.document.documentElement.clientHeight;
  63.             w = window.content.document.documentElement.clientWidth;
  64.         }
  65.         else
  66.         {
  67.             h = window.content.document.body.clientHeight;
  68.             w = window.content.document.body.clientWidth;
  69.         }
  70.         
  71.         this.grab(window.content,x,y,w,h);
  72.     },
  73.  
  74.     grab: function(win, x, y, w, h)
  75.     {
  76.  
  77.         // Figure out how big to make the canvas
  78.         const MAX_DIM = 2800;
  79.         var canvasW = w;
  80.         var canvasH = h;
  81.         if( w>MAX_DIM || h>MAX_DIM)
  82.         {
  83.             if(w > h) 
  84.             {
  85.                 canvasW = MAX_DIM;
  86.                 canvasH = canvasW*h/w;
  87.             }
  88.             else 
  89.             {
  90.                 canvasH = MAX_DIM;
  91.                 canvasW = canvasH*w/h;
  92.             }
  93.         }
  94.  
  95.         // Make the canvas
  96.         var canvas = document.createElementNS('http://www.w3.org/1999/xhtml','canvas');
  97.         canvas.style.width = canvasW+"px";
  98.         canvas.style.height = canvasH+"px";
  99.         canvas.width = canvasW;
  100.         canvas.height = canvasH;
  101.         var ctx = canvas.getContext("2d");
  102.         ctx.clearRect(0, 0, canvasW, canvasH);
  103.         ctx.save();
  104.         ctx.scale(canvasW/w, canvasH/h);
  105.         ctx.drawWindow(win, x, y, w, h, "rgb(255,255,255)");
  106.         ctx.restore();
  107.         
  108.         // Save it to picnik
  109.         this.saveDataURL(canvas.toDataURL("image/png", ""), "image/png");
  110.     },
  111.  
  112.     showGears: function()
  113.     {
  114.         var doc = window.content.document
  115.         var bod = doc.getElementsByTagName('body')[0];
  116.         var shadow = doc.createElement('div');
  117.         shadow.id = 'picnik-overlay-shadow';
  118.         shadow.style.opacity = "0.7"
  119.         shadow.style.MozOpacity = shadow.style.opacity
  120.         shadow.style.top="0"
  121.         shadow.style.left="0"
  122.         shadow.style.width="100%"
  123.         shadow.style.height="100%"
  124.         shadow.style.display="block"
  125.         shadow.style.position="fixed"
  126.         shadow.style.backgroundColor="#000";
  127.         shadow.style.zIndex = "9997"
  128.         bod.appendChild(shadow);
  129.  
  130.         var gears_img = doc.createElement('img');
  131.         gears_img.src = "chrome://picnik/content/gears.gif";
  132.  
  133.         var gears = doc.createElement('div');
  134.         gears.id = 'picnik-overlay-gears';
  135.         gears.style.top="50%"
  136.         gears.style.left="50%"
  137.         gears.style.marginTop = -gears_img.height/2 + "px";
  138.         gears.style.marginLeft = -gears_img.width/2 + "px";
  139.         gears.style.display="block"
  140.         gears.style.position="fixed"
  141.     //    gears.style.backgroundColor="#ccc";
  142.         gears.style.zIndex = "9998"
  143.         gears.appendChild(gears_img)
  144.         bod.appendChild(gears);
  145.  
  146.         return {body: bod, div_shadow: shadow, div_gears: gears}
  147.     },
  148.  
  149.     hideGears: function(gears)
  150.     {
  151.         gears.body.removeChild( gears.div_shadow )
  152.         gears.body.removeChild( gears.div_gears )
  153.     },
  154.  
  155.     saveDataURL: function(url, mimeType)
  156.     {
  157.         const MULTI                        = "@mozilla.org/io/multiplex-input-stream;1";
  158.         const STRINGIS                    = "@mozilla.org/io/string-input-stream;1";
  159.         const IOSVC                        = "@mozilla.org/network/io-service;1"
  160.         const nsIMultiplexInputStream    = Components.interfaces.nsIMultiplexInputStream;
  161.         const nsIStringInputStream        = Components.interfaces.nsIStringInputStream;
  162.         const nsIIOService                = Components.interfaces.nsIIOService
  163.         const BOUNDARY                    = "345823569845694578678";
  164.  
  165.         var gears = this.showGears()
  166.  
  167.         // Build the mime header
  168.         var header = new String();
  169.         header += "\r\n";
  170.         header += "--" + BOUNDARY + "\r\n";
  171.         header += "Content-disposition: form-data;name=\"_returntype\"\r\n\r\ntext\r\n";
  172.         header += "--" + BOUNDARY + "\r\n";
  173.         header += "Content-disposition: form-data;name=\"_apikey\"\r\n\r\n"+picnikCommon.APIKey+"\r\n";
  174.         header += "--" + BOUNDARY + "\r\n";
  175.         header += "Content-disposition: form-data;name=\"_file\";filename=\"snapshot.png\"\r\n";
  176.         header += "Content-Type: "+mimeType+"\r\n";
  177.         header += "\r\n";
  178.         var header_stream = Components.classes[STRINGIS].createInstance(nsIStringInputStream);
  179.         header_stream.setData(header, header.length);
  180.  
  181.         // create a data url from the canvas and then create a stream
  182.         var io = Components.classes[IOSVC].getService(nsIIOService);
  183.         var channel = io.newChannelFromURI( io.newURI(url, "UTF8", null) );
  184.         var data_stream = channel.open();
  185.  
  186.         // Ending MIME boundary
  187.         var end_stream = Components.classes[STRINGIS].createInstance(nsIStringInputStream);
  188.         var bs = new String("\r\n--" + BOUNDARY + "--\r\n");
  189.         end_stream.setData(bs, bs.length);
  190.  
  191.         // Stick 'em all together
  192.         var mux_stream = Components.classes[MULTI].createInstance(nsIMultiplexInputStream);
  193.         mux_stream.appendStream(header_stream);
  194.         mux_stream.appendStream(data_stream);
  195.         mux_stream.appendStream(end_stream);
  196.  
  197.         // Finally, POST it to picnik!
  198.         var req = new XMLHttpRequest();
  199.         req.open('POST', picnikCommon.baseURL+"service", true);
  200.         req.onreadystatechange = function(){ picnikScreenGrab.requestState(req, gears); };
  201.         req.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
  202.         req.setRequestHeader("Content-Length", mux_stream.available() );
  203.         req.send(mux_stream);
  204.     },
  205.  
  206.     requestState: function(req, gears)
  207.     {
  208.         if( req.readyState == 4 )
  209.         {
  210.             if( req.status == 200 )
  211.             {
  212.                 result = req.responseText;
  213.                 gBrowser.selectedTab = gBrowser.addTab(result);
  214.                 //gBrowser.addTab(result);
  215.             }
  216.             else
  217.             {
  218.                 alert('There was a problem with the request\nstatus=' + req.status);
  219.             }
  220.             picnikScreenGrab.hideGears(gears)
  221.         }
  222.     }
  223. };
  224.  
  225. window.addEventListener("load", picnikScreenGrab.onLoad, false);
  226.